package com.icangya.Utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

public class UploadUtils {



    private Logger logger = LoggerFactory.getLogger(this.getClass());


    //上传图片
    public  static String uploadImg(String path,HttpServletRequest request, MultipartFile file ){
        //上传的目录地址
        String uploadDir= request.getSession().getServletContext().getRealPath("/")+path;
        //如果目录不存在，自动创建文件夹
        File dir=new File(uploadDir);
        if(!dir.exists()){
            dir.mkdir();
        }
        String suffix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        //上传文件名
        String filename= UUID.randomUUID()+suffix;
        //String filename=file.getOriginalFilename();

        //服务器端保存的文件对象
        File serverFile=new File(uploadDir+filename);

        //将上传的文件写入到服务器端文件内
        try {
            file.transferTo(serverFile);
        } catch (IOException e) {
            e.printStackTrace();//上传失败
        }
        return path+filename;
    }

    /*
    *
    * 上传图片的方法
    *
    * */
    public static  String upload(HttpServletRequest request,  String image,String path){


        image=image.replace("[douhao]",",");
        String[] img=image.split(",");
//        String suffix=img[0].substring(img[0].indexOf("/")+1,img[0].indexOf(";"));
        String suffix=img[0].split("/")[1].split(";")[0];
//       String name= image.substring(image.lastIndexOf(",")+1);
        String name= img[1];
//        path="upload/";
        //上传的目录地址
        String uploadDir= request.getSession().getServletContext().getRealPath("/")+path;
        //如果目录不存在，自动创建文件夹
        File dir=new File(uploadDir);
        if(!dir.exists()){
            dir.mkdir();
        }

        //上传文件名
        String filename= UUID.randomUUID()+"."+suffix;
        String uploadImgUrl="";
        BASE64Decoder decoder=new BASE64Decoder();
        try{
            //Base64解码
            byte[] b=decoder.decodeBuffer(name);
            for (int i=0;i<b.length;++i){
                if(b[i]<0){
                    b[i]+=256;//调整异常数据
                }
            }
            //生成jpeg图片
            OutputStream out = new FileOutputStream(uploadDir+filename);
            out.write(b);
            out.flush();
            out.close();
            uploadImgUrl=path+filename;
        }catch(Exception e){
            e.printStackTrace();
        }

        return uploadImgUrl;
//        return null;
    }


//        public static  boolean missionUtil(User user,long missionId,IMissionService iMissionService){
//
//                    //获取可领取的奖励
//                   List<FbMissionAward> missionAwards = iMissionService.getMissionAwardStatus();
//                   if(missionAwards!=null){
//                       Random random=new Random();
//                       int result=random.nextInt(missionAwards.size());
//
//                       FbMissionAward fbMissionAward=missionAwards.get(result);  //获取奖励信息
//                       //添加转盘记录
//                       FbTurntableRecord turntableRecord=new FbTurntableRecord();
//                        turntableRecord.setUid(user.getId());
//                        turntableRecord.setNickName(user.getNickName());
//                        turntableRecord.setOpenId(user.getOpenId());
//                        turntableRecord.setHeadImgUrl(user.getHeadImgUrl());
////                        turntableRecord.setMissionId(userMission.getMissionId());
////                        turntableRecord.setMissionContent(userMission.getMissionContent());
//                        turntableRecord.setAwardId(fbMissionAward.getAwardId());
//                        turntableRecord.setAwardContent(fbMissionAward.getAwardContent());
//                        turntableRecord.setStatus(0);
//                        turntableRecord.setCreateTime(new Timestamp(new Date().getTime()));
//
//                       FbTurntableRecord turntable= iMissionService.saveFbTurntableRecord(turntableRecord);
//                       if(turntable!=null){
//                           return true;  // 完成任务，增加转盘记录
//                       }
//                   }
//
//
//
//            return false;
//        }


}

